package com.bwie.esdemo0122.service.impl;

import com.bwie.esdemo0122.service.EsIndexService;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Service
public class EsIndexServiceImpl implements EsIndexService {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @Override
    public boolean createIndex(Map<String,Object> indexInfo) {
        String indexName = (String)indexInfo.get("indexName");
        Map<String,Object> mappings = (Map<String, Object>)indexInfo.get("mappings");
        //参数校验
        if (StringUtils.isEmpty(indexName)) {
            System.out.println("索引名称不能为空");
            return false;
        }
        //执行请求
        try {
            //校验索引是否存在
            if (existsIndex(indexName)) {
                return false;
            }
            //构建一个创建请求
            CreateIndexRequest request = new CreateIndexRequest(indexName);
            if (mappings != null){
                request.mapping(mappings);
            }
            CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
            if (createIndexResponse.isAcknowledged()) {
                System.out.println("创建索引成功");
                return true;
            }
        } catch (IOException e) {
            e.printStackTrace();
            //TODO 日志不用sout打印
            System.out.println("创建索引失败");
            return false;
        }
        //默认返回失败
        return false;
    }

    @Override
    public boolean deleteIndex(String indexName) {
        //参数校验
        if (StringUtils.isEmpty(indexName)) {
            System.out.println("索引名称不能为空");
            return false;
        }
        //判断索引是否存在
        if (existsIndex(indexName)) {
            //执行请求
            try {
                //构建删除请求
                DeleteIndexRequest request = new DeleteIndexRequest(indexName);
                AcknowledgedResponse delete = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
                if (delete.isAcknowledged()) {
                    System.out.println("删除索引成功");
                    return true;
                }
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("删除索引失败");
                return false;
            }
        }
        return false;
    }

    public boolean existsIndex(String indexName) {
        //构建一个查询请求
        GetIndexRequest getIndexRequest = new GetIndexRequest(indexName);
        try {
            boolean exists = restHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
            return exists;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
}
